package com.wink_172.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import com.wink_172.adapter.HBomeListAdapter
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityCopyWritingListBinding
import com.wink_172.library.activity.PlayImageActivity
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.adapter.HomeListAdapter
import com.wink_172.library.model.BaseDataItem
import com.wink_172.library.model.json.FileData
import com.wink_172.library.utils.CommonUtils
import com.wink_172.library.utils.HttpRequest.get_test_imge_listbb
import com.wink_172.library.view.ActionBarView
import com.wink_172.model.Constants
import com.wink_172.model.Constants.Copywriting_List

/**
 * 文案列表
 */
class CopyWritingListActivity : SmartActivity() {
    var binding: ActivityCopyWritingListBinding? = null
    var first_time = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_copy_writing_list)
        initView()
        init()
    }

    override fun handlerCallBack(msg: Message?) {
        super.handlerCallBack(msg)
        if (msg!!.what == Constants.EventEnum.更新下载状态.ordinal) {
        }
    }

    fun init() {
        mode = intent.getIntExtra(com.wink_172.library.model.Constants.PARAM_DATA1, 0)

        get_test_imge_listbb(object  : com.wink_172.library.callback.ICallback{
            override fun onSendEvent(event: Int, vararg args: Any?) {
                if (event == com.wink_172.library.model.Constants.EventEnum.网络请求成功.ordinal) {



                    val result = args[0] as String
                    Log.e(TAG, "onSendEvent: ====>>获取列表result:" + result)
                    val list: MutableList<String> = ArrayList()
                    list.addAll(result.split("\n"))  // Split by newline and add all to list
                    initImageList(list);
                }
            }
        })

    }

    fun initView() {
//        binding!!.actionBar.updateAllContent(R.mipmap.icon_back, "", "去下载页")
//        binding!!.actionBar.setCallback(object : ActionBarView.ICallback {
//            override fun onSendEvent(event: Int, content: String?) {
//                if (event == ActionBarView.LEFT_BTN) {
//                    finish()
//                } else if (event == ActionBarView.RIGHT_BTN) {
//                }
//            }
//        })
        if (true) {
            val list2: List<Any?> = ArrayList()
            val homeListAdapter2 = HBomeListAdapter(this, list2, binding!!.listView2)
            binding!!.listView2.init(homeListAdapter2, false)
            val gridLayoutManager2 =
                GridLayoutManager(activity, 1, GridLayoutManager.VERTICAL, false)
            gridLayoutManager2.spanSizeLookup = object : SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
//                if (position < 7 || position > 14) {
//                    return 3;
//                }
                    return 1
                }
            }
            binding!!.listView2.layoutManager = gridLayoutManager2
        }
    }


    fun initImageList(file_list: List<String?>) {
        val list: MutableList<Any> = ArrayList()
        val homeListAdapter = binding!!.listView2.adapter as HBomeListAdapter?
        for (i in file_list.indices) {
            val baseDataItem = BaseDataItem(i, Copywriting_List)

            homeListAdapter!!.mOnItemClickListener = object : HomeListAdapter.OnItemClickListener {
                override fun onItemClick(view: View?, pos: Int) {

                    Log.e(TAG, "onItemClick: ===="+pos )
                    CommonUtils.copyStr(file_list[pos] as String)
                    CommonUtils.showToast("文案复制成功"+file_list[pos] as String)
                }
            }

            baseDataItem.content=file_list[i]
            baseDataItem.setObjectData(file_list[i])
            list.add(baseDataItem)
        }
        homeListAdapter!!.isListSelect = homeListAdapter.isListSelect
        homeListAdapter.swapContent(list)
        homeListAdapter.notifyDataSetChanged()
    }





    companion object {
        private const val TAG = "CopyWritingListActivity"
        fun startActivity(context: Activity, vararg args: Any) {
            val intent = Intent(context, CopyWritingListActivity::class.java)
            val mode = args[0] as Int
            intent.putExtra(com.wink_172.library.model.Constants.PARAM_DATA1, mode)
            when (mode) {
                0 -> {}
            }
            context.startActivity(intent)
        }
    }
}